﻿<%@ WebHandler Language="C#" Class="SaveDataRow" %>

using System;
using System.Web;
using System.Data;
using System.Data.OracleClient;

public class SaveDataRow : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
		context.Response.ContentType = "text/plain";
		string dataToParse = context.Request.Params["command"];

		string[] items = dataToParse.Split(',');
		decimal enrollmentId = decimal.Parse(items[7]);
		DateTime start = DateTime.Parse(items[8]);
		DataLayer dal = new DataLayer();
		string message;
		DataTable codes = dal.GetCodes(PAConfiguration.ConnectionString, dal.GetCurrent(PAConfiguration.ConnectionString, out message).TermId, out message);
		PALogManager.MessageLog(message);
		string[] comments = items[9].Split(new string[] { "###" }, StringSplitOptions.None);
		start = start.AddDays(-1);

		using (OracleConnection connection = new OracleConnection(PAConfiguration.ConnectionString))
		{
			connection.Open();
			OracleTransaction trans = connection.BeginTransaction();
			try
			{
				for (int i = 0; i < 7; i++)
				{
					start = start.AddDays(1);
					string scodes = items[i];
					string code = string.Empty;

					if (scodes.Length > 0)
					{
						code = scodes.Substring(0, 1);
						scodes = scodes.Substring(1, scodes.Length - 1);
					}
					
					decimal attCodeId = 0;

					foreach (DataRow dr in codes.Rows)
					{
						if (!dr.IsNull("ATT_CODE") && ((string)dr["ATT_CODE"] == code))
						{
							attCodeId = (decimal)dr["ATTENDANCE_CODE_ID"];
							continue;
						}
					}

					if (code == "N")
						continue;
					
					if (attCodeId == 0)
						continue;


					dal.SetAttendance(connection, trans, enrollmentId, start, attCodeId, comments[i], scodes, out message);
					PALogManager.MessageLog(PAConfiguration.ConnectionString);
				}

				trans.Commit();
			}
			catch (System.Exception ex)
			{
				trans.Rollback();
				throw ex;
			}


			connection.Close();
		}
		context.Response.ContentType = "text/xml";
		context.Response.Write("<status>Ok</status>");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}